import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {InspectionService} from "../inspection.service";
import {StorageService} from "../../services/storage.service";
import {ConfirmationService} from "primeng/primeng";

@Component({
  selector: 'app-inspectionview-detail-dialog',
  templateUrl: './inspectionview-detail-dialog.component.html',
  styleUrls: ['./inspectionview-detail-dialog.component.scss']
})
export class InspectionviewDetailDialogComponent implements OnInit {
  @Output() closeMask = new EventEmitter();
  @Output() saveIntervalInspectionOrder = new EventEmitter();
  @Input() inpectionOrder;
  queryModel;
  cols;
  display;
  chooseCids =[];
  inspections;
  totalRecords;
  width;
  windowSize;
  selectInsepections;
  constructor(
    private inspectionService: InspectionService,
    private storageService: StorageService,
    private confirmationService:ConfirmationService  ) { }

  ngOnInit() {
    this.windowSize = window.innerWidth;
    if(this.windowSize<1024){
      this.width = this.windowSize*0.9;
    }else{
      this.width = this.windowSize*0.8;
    }
    this.queryModel={
      "condition":{
        "inspection_mission_cid":this.storageService.getCurrentInspectionMissionCid('permissioncid'),
      },
      "page":{
        "page_size":"10",
        "page_number":"1"
      }
    };
    this.display = true;
    this.cols = [
      {field: 'region', header: '区域',editable:false},
      {field: 'object', header: '巡检设备',editable:false},
      {field: 'content', header: '巡检内容',editable:false},
      {field: 'object_type', header: '类型',editable:false},
      {field: 'reference_value', header: '参考值',editable:false},
      {field: 'real_value', header: '巡检记录值',editable:this.isEditable(this.inpectionOrder['status'])},
      {field: 'remark', header: '备注',editable:this.isEditable(this.inpectionOrder['status'])},
    ]
    this.queryInsepection();
  }
  isEditable(val){
    switch (val){
      case '正在执行' :
        return true
      case '未巡检' :
        return false
      case '已巡检' :
        return false
      case '有故障' :
        return false
      case '延时' :
        return true
      case '漏检' :
        return false
    }
  }
//分页查询
  paginate(event) {
    // event.first = Index of the first record
    // event.rows = Number of rows to display in new page
    // event.page = Index of the new page
    // event.pageCount = Total number of pages
    this.chooseCids =[]
    this.queryModel.page.page_number = String(++event.page);
    this.queryModel.page.page_size = String(event.rows);
    this.queryInsepection()
  }
  queryInsepection(){
    this.inspectionService.queryInspectionTaskDetail(this.queryModel).subscribe(data=>{
      this.inspections = data.items;
      this.totalRecords = data.page.total;
      if(!this.inspections){
        this.inspections = [];
        this.selectInsepections =[]
      }
    });
  }

  closeIntervalMask(bool){
    this.closeMask.emit(bool);
  }
  tableEditComplete($event){
    console.log($event)
  }
  saveIntervalOrder(){
    console.log(this.inspections);
    let inpections = this.inspectionService.resverInspectionIntervalSubmitData(this.inspections);
    for(let i = 0;i<inpections.length;i++){
      inpections[i]['status'] = '已完成';
      if(!inpections[i]['real_value']||!inpections[i]['real_value'].trim()){
        this.confirmationService.confirm({
          message: '巡检记录值不能为空！',
          rejectVisible:false,
        })
        return
      }
    }
    this.inspectionService.addInspectionTaskItems(inpections).subscribe(()=>{

    })
    this.saveIntervalInspectionOrder.emit(false);
  }

}
